#region Using Directives

using System;
using System.IO;
using System.Windows.Forms;
using System.Globalization;
using Microsoft.VisualStudio.Modeling;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;
using Microsoft.Practices.RecipeFramework.Services;
using Microsoft.Practices.RecipeFramework.VisualStudio;
using Microsoft.Practices.RecipeFramework.VisualStudio.Templates;
using EnvDTE;
using EastBancTech.InstallerDefinition;
using Microsoft.Practices.Modeling.Dsl.Integration.Helpers;

#endregion

namespace EBT.Deployment.SoftwareFactory.Actions
{
    /// <summary>
    /// Adds new WebApplication component to Installer DSL model
    /// </summary>
    [ServiceDependency(typeof(DTE))]
    public class AddWebApplicationToModel : AddComponentBase<WebAspApplication>
    {
        String virtualDirectory;
        String webAppName;
        IISApplicationPool appPool;
        WebSite webSite;
      
        [Input(Required = true)]
        public IISApplicationPool AppPool
        {
            get { return appPool; }
            set { appPool = value; }
        }

        [Input(Required = true)]
        public WebSite WebSite
        {
            get { return webSite; }
            set { webSite = value; }
        }


        [Input(Required = true)]
        public String VirtualDirectory
        {
            get { return virtualDirectory; }
            set { virtualDirectory = value; }
        }

        [Input(Required = true)]
        public String WebAppName
        {
            get { return webAppName; }
            set { webAppName = value; }
        }
           
        protected override LinkedElementCollection<WebAspApplication> LinkedCollection
        {
            get { return ComponentPackage.IISApplications; }
        }

        protected override WebAspApplication CreateComponent(ComponentPackage cmpPackage)
        {
            WebAspApplication aspApp = cmpPackage.Store.ElementFactory.CreateElement(WebAspApplication.DomainClassId) as WebAspApplication;
            aspApp.Name = WebAppName;
            aspApp.VirtualPath = VirtualDirectory;

            IIS iis = DomainModelHelper.GetElement<IIS>(WixPackageStore) as IIS;
            Guard.ArgumentNotNull(iis, "IIS  Host");

            IISHosted iisHosted = cmpPackage.Store.ElementFactory.CreateElementLink(IISHosted.DomainClassId,
                new RoleAssignment(IISHosted.ComponentDomainRoleId, aspApp), new RoleAssignment(IISHosted.ComponentHostDomainRoleId, iis)) as IISHosted;
            iisHosted.AppPool = AppPool;
            iisHosted.WebSite = WebSite;

            return aspApp;
        }
    }
}
